#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    structToJson();
    jsonToStruct();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::jsonToStruct()
{
    QString jsonFile = QApplication::applicationDirPath() + "/testJson.json";
    QFile json (jsonFile);
    if(json.open(QIODevice::ReadOnly))
    {
        TestJsonData jsonData;
        jsonData.fromJson(json.readAll());
        json.close();
        //qDebug() << jsonData;
    }
}

void MainWindow::structToJson()
{
    // 填充数据
    TestCustomObject customData;
    customData.code = 10001;
    customData.msg = "test data";
    QStringList list;
    list << "list1" << "list2" << "list3";
    TestJsonData jsonData(123456, "hello word", true, 4.567, list, customData);
    QString jsonFile = QApplication::applicationDirPath() + "/testJson.json";
    QFile json (jsonFile);
    if(json.open(QIODevice::WriteOnly))
    {
        const QByteArray& arrayData = QSerializer::toByteArray(jsonData.toJson());
        json.write(arrayData);
        json.close();
    }
}


